package dao;

import tool.Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeleteUserDao {
    Util util = new Util();
    Connection con = null;

    public String[] getUsers1(String username) {
        con = util.getConnection();
        List<String> userList = new ArrayList<>();
        String sql = "select username1 from friendships where username2 = ? and status=0";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String userName = resultSet.getString("username1");
                userList.add(userName);
            }
            // 将列表转换为字符串数组
            String[] usersArray = new String[userList.size()];
            usersArray = userList.toArray(usersArray);
            return usersArray;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public String[] getUsers2(String username) {
        con = util.getConnection();
        List<String> userList = new ArrayList<>();
        String sql = "select username2 from friendships where username1 = ? and status=0";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String userName = resultSet.getString("username2");
                userList.add(userName);
            }
            // 将列表转换为字符串数组
            String[] usersArray = new String[userList.size()];
            usersArray = userList.toArray(usersArray);
            return usersArray;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void deleteUser(String username1, String username2) {
        con = util.getConnection();
        String sql = "update friendships set status = 1 where (username1 = ? and username2 = ?) or (username1=? and username2=?)";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, username1);
            preparedStatement.setString(2, username2);
            preparedStatement.setString(3, username2);
            preparedStatement.setString(4, username1);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean checkStatus(String username1, String username2) {
        con = util.getConnection();
        String sql = "select * from xyz.friendships where( (username1 = ? and username2 = ?) or (username1=? and username2= ?)) and status=0";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, username1);
            preparedStatement.setString(2, username2);
            preparedStatement.setString(3, username2);
            preparedStatement.setString(4, username1);
            ResultSet resultSet = preparedStatement.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void update(String username1, String username2) {
        con = util.getConnection();
        String sql = "update message set status = 1 where ((from_name =? and to_name =?) or (from_name =? and to_name =?))";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, username1);
            preparedStatement.setString(2, username2);
            preparedStatement.setString(3, username2);
            preparedStatement.setString(4, username1);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}